package com.lmc.edrive;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.lmc.edrive.database.DbManager;

/**
 * Servlet implementation class Vehicle
 */
@WebServlet("/Vehicle")
public class Vehicle extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Vehicle() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String method=request.getParameter("method");
		if(method.equals("add")) {
			addVehicle(request, response);
		}else if(method.equals("get")) {
			getVehicle(request, response);
		} else if (method.equals("delete")) {
			deleteVehicle(request,response);
		}
	}
		// TODO Auto-generated method stub
	

	private void deleteVehicle(HttpServletRequest request,
			HttpServletResponse response) {
		String emailId=request.getParameter("emailid");
		String vehiclename=request.getParameter("vhname");
		DbManager db=DbManager.getInstance();
		if (emailId!=null && db.isUserExist(emailId)) {
			db.deleteVehicle(emailId, vehiclename);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}
	
	private void addVehicle(HttpServletRequest request, HttpServletResponse response) throws IOException{
	
		String emailId=request.getParameter("emailid");
		String vehiclename=request.getParameter("vhname");
		String modelStr=request.getParameter("model");
		String mileageStr=request.getParameter("mil");
		String type=request.getParameter("type");
		int model=Integer.parseInt(modelStr);
		float mileage=Float.parseFloat(mileageStr);
		PrintWriter pw=response.getWriter();
		DbManager db=DbManager.getInstance();
		if (emailId!=null && db.isUserExist(emailId)) {
			db.addVehicle(emailId, vehiclename, model, mileage, type);
			pw.println("Vehicle Added");
			
		}else {
			pw.println("User Doesn't Exist");
		}
	
		
	
		
		
		}
	private void getVehicle(HttpServletRequest request, HttpServletResponse response) throws IOException{
		String emailId=request.getParameter("emailid");
		PrintWriter pw=response.getWriter();
		DbManager db=DbManager.getInstance();
		if (emailId!=null && db.isUserExist(emailId)) {
			List<com.lmc.edrive.data.Vehicle> vehicles =db.getVehicles(emailId);
			// convert java object to JSON format,
			// and returned as JSON formatted string
			Gson gson = new Gson();
			String json = gson.toJson(vehicles);
			pw.println(json);
			
		}else {
			pw.println("User Doesn't Exist");
		}
	
	}
	


}
